package cn.com.guage.transtation.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class TccTransactionDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 插入事务记录（幂等性控制）
    public boolean insertTransaction(String txId, Integer status) {
        String sql = "INSERT INTO tcc_transaction (tx_id, status) VALUES (?, ?) " +
                "ON DUPLICATE KEY UPDATE status = VALUES(status)";
        int rows = jdbcTemplate.update(sql, txId, status);
        return rows > 0;
    }

    // 查询事务状态
    public Integer getTransactionStatus(String txId) {
        String sql = "SELECT status FROM tcc_transaction WHERE tx_id = ?";
        try {
            return jdbcTemplate.queryForObject(sql, Integer.class, txId);
        } catch (Exception e) {
            return null;
        }
    }
}